<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="closeDialog">
    <!-- 多选框组 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox :label="item.id" v-for="item in roleList" :key="item.id">{{item.name}}</el-checkbox>
    </el-checkbox-group>
    <!-- 底部 -->
    <el-row slot="footer">
      <el-col :span="6">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from "@/api/setting";
import { getUserDetailById } from "@/api/user";
import { assignRoles } from "@/api/employees";

export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false,
    },
    userId: {
      // 当前要处理的用户id
      type: String,
      required: true,
      default: null,
    },
  },
  data() {
    return {
      roleList: [], //角色列表
      roleIds: [], //多选框选择的值
    };
  },
  created() {
    // 获取所有角色
    this.getRoleList();
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList({
        page: 1,
        pagesize: 20,
      });
      this.roleList = rows;
    },
    // 获取用户已有的角色信息,让父组件调用
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id);
      this.roleIds = roleIds; // 赋值本用户的角色
    },
    //
    closeDialog() {
      this.$parent.closeRoleDialog();
    },
    // 更新用户的角色
    async btnOk() {
      await assignRoles({
        id: this.userId,
        roleIds: this.roleIds,
      });
      // 关闭弹层
      // this.closeDialog();
      this.$emit("update:showRoleDialog", false);
      // 重置空数组
      this.roleIds = [];
    },
  },
};
</script>

<style lang="scss" scoped>
</style>